VKPlayer = (function($, self, undefined) {
  	/**
     * Коллекции (Collections)
     */
    self.Collections = {

        /* Collections.Tracks */
        Tracks: Backbone.Collection.extend({
            model: self.Models.Track,
            initialize: function() {
                var tracks = this;
                if (tracks.length) {
                    var hashes = tracks.pluck('hash');
                    getMyFeedbacks(hashes)
                        .done(function(feedbacks) {
                            _.each(feedbacks, function(data) {
                                var trackModel = _.first(tracks.getByHash(data.hash));
                                if (!_.isEmpty(trackModel)) {
                                    trackModel.set({attitude: data.attitude});
                                }
                            });

                        });
                }
            },
			
            getById : function(aid) {
                return this.filter(function(model) {
                    return model.get('aid') == aid;
                });
            },

			random: function(model, max) {
				var index = Math.floor(Math.random() * (max - 1));
                model = this.at(index);
                if (model.get('removed'))
                    model = this.random(model, max);

                return model;
            },
            next: function(model) {
                model = this.at(this.indexOf(model) + 1);
                if (_.isEmpty(model))
                    model = this.first();

                if (model.get('removed'))
                    model = this.next(model);

                return model;
            },

            prev: function(model) {
                model = this.at(this.indexOf(model) - 1);
                if (_.isEmpty(model))
                    model = this.last();

                if (model.get('removed'))
                    model = this.next(model);

                return model;
            },

            prepend : function(model) {

            },

            getByHash: function(hash) {
                return this.filter(function(model) {
                    return model.get('hash') == hash;
                });
            }

        }),
        /* Collections.Albums */
        Albums: Backbone.Collection.extend({
            model: self.Models.Album,
            getById : function(album_id) {
                return this.filter(function(model) {
                    return model.get('album_id') == album_id;
                });
            }
        }),
        /* Collections.Users */
        Users: Backbone.Collection.extend({
            model: self.Models.User,
            getById : function(uid) {
                return this.filter(function(model) {
                    return model.get('uid') == uid;
                });
            }
        }),

        /* Collections.Tracklists */
        Tracklists: Backbone.Collection.extend({
            model: self.Models.Tracklist
        }),

		/* Collections.Links */
        Links: Backbone.Collection.extend({
            model: self.Models.Link
        }),

		/* Collections.VideosVk */
		VideosVk: Backbone.Collection.extend({
            model: self.Models.VideoVk
        })

    };
	return self;
	
})(jQuery, VKPlayer || {});
